package cn.monkey.io.transport.core;

import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @param <EXCHANGE> Exchange仓库
 */
public interface ExchangeRepository<EXCHANGE extends Exchange> {

    /**
     * @param id {@link EXCHANGE#id()}
     */
    Optional<EXCHANGE> select(String id);

    Collection<EXCHANGE> selectAll();

    default Collection<EXCHANGE> select(Predicate<EXCHANGE> p) {
        return this.selectAll().stream().filter(p).collect(Collectors.toSet());
    }

    default Optional<EXCHANGE> selectOne(Predicate<EXCHANGE> p) {
        return this.selectAll().stream().filter(p).findFirst();
    }
}
